package xin.marcher.module.diplomacy.manager.custom.db;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Sets;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import xin.marcher.module.common.enums.DelFlagEnum;
import xin.marcher.module.diplomacy.domain.entity.FrontCategoryDO;
import xin.marcher.module.diplomacy.manager.common.cache.RedisStringCache;
import xin.marcher.module.diplomacy.manager.common.db.AbstractRedisStringDatabase;
import xin.marcher.module.diplomacy.mapper.FrontCategoryMapper;

import javax.annotation.Resource;
import java.util.*;

import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toSet;

/**
 * 前台类目信息
 */
@Slf4j
@Service("frontCategoryStringDatabase")
public class FrontCategoryStringDatabase extends AbstractRedisStringDatabase<FrontCategoryDO> {

    @Resource
    private FrontCategoryMapper frontCategoryMapper;

    /**
     * 表名
     */
    private static final String TABLE_NAME = "front_category";

    /**
     * 主键ID
     */
    public static final String ID = "id";

    /**
     * 数据源单表
     *
     * @return java.lang.String
     */
    @Override
    public String getTableName() {
        return TABLE_NAME;
    }

    /**
     * 数据源全量表
     *
     * @return java.util.Set<java.lang.String>
     */
    @Override
    public Set<String> getTableNameSet() {
        return Sets.newHashSet(TABLE_NAME);
    }

    /**
     * 获取表数据
     *
     * @param tableFieldsMap key 数据库字段，value 字段值
     * @param queryType      查询类型，多数据查询时区分
     * @return java.util.Optional<xin.marcher.module.diplomacy.domain.entity.FrontCategoryDO>
     */
    @Override
    public Optional<FrontCategoryDO> getTableData(Map<String, Object> tableFieldsMap, String queryType) {
        if (tableFieldsMap.containsKey(ID)) {
            QueryWrapper<FrontCategoryDO> queryWrapper = new QueryWrapper<>();
            queryWrapper.in("ID", Sets.newHashSet(Integer.valueOf(tableFieldsMap.get(ID).toString())));
            List<FrontCategoryDO> frontCategoryDOList = frontCategoryMapper.selectList(queryWrapper);

            if (!CollectionUtils.isEmpty(frontCategoryDOList)) {
                FrontCategoryDO doBase = frontCategoryDOList.get(0);
                if (Objects.equals(DelFlagEnum.EFFECTIVE.getCode(), doBase.getDelFlag())) {
                    return Optional.of(doBase);
                }
            }
            return Optional.empty();
        }
        throw new UnsupportedOperationException();
    }

    /**
     * 获取表集合数据
     *
     * @param tableFieldsMap
     * @param queryType
     * @return java.util.Optional<java.util.List < T>>
     */
    @Override
    public Optional<List<FrontCategoryDO>> listTableData(Map<String, Object> tableFieldsMap, String queryType) {
        return Optional.empty();
    }

    /**
     * 根据ID批量获取数据
     *
     * @param idSet     表主键ID集合
     * @param queryType 查询类型，多数据查询时区分
     * @return java.util.Optional<xin.marcher.module.diplomacy.manager.common.source.RedisStringSource < xin.marcher.module.diplomacy.domain.entity.FrontCategoryDO>>
     */
    @Override
    public Optional<RedisStringCache<FrontCategoryDO>> listTableDataByIdSet(Set<Long> idSet, String queryType) {
        QueryWrapper<FrontCategoryDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("ID", idSet);
        List<FrontCategoryDO> frontCategoryDOList = frontCategoryMapper.selectList(queryWrapper);
        if (!CollectionUtils.isEmpty(frontCategoryDOList)) {
            RedisStringCache redisStringCache = new RedisStringCache();
            List<FrontCategoryDO> addList = frontCategoryDOList.stream().filter(doBase -> doBase.getDelFlag().equals(DelFlagEnum.EFFECTIVE.getCode())).collect(toList());
            Set<String> deleteSet = frontCategoryDOList.stream().filter(doBase -> doBase.getDelFlag().equals(DelFlagEnum.DISABLED.getCode())).map(doBase -> String.valueOf(doBase.getId())).collect(toSet());
            redisStringCache.setAddList(addList);
            redisStringCache.setDeleteSet(deleteSet);
            return Optional.of(redisStringCache);
        }
        return Optional.empty();
    }

    @Override
    public Optional<RedisStringCache<FrontCategoryDO>> listTableDataByIdSet(List<Long> idSet, String queryType) {
        return Optional.empty();
    }


}
